<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;


class CurrentDay extends Authenticatable
{
    use Notifiable;

	// 当日订单统计
	public static function getCurrentDayOrder(){
		$data = array();
		$sql_total = " SELECT count(*) as order_total FROM c_order WHERE  state>1 AND state<=4 ";
		$order_total = DB::select($sql_total);// 截止当前订单总数
		if(!$order_total)$order_total =  array();
		$data['order_total'] = isset($order_total[0]->order_total) ? $order_total[0]->order_total : 0;
		return $data;
	}

	// 当日收益额
	public static function getCurrentDayMoney($day = 'today'){
		$data = array();
		$sql_total = " SELECT SUM(order_money) as money_total FROM  c_order WHERE  state >1 AND state <=4 "; //总额
		$sql_today = " SELECT SUM(order_money) as money_t FROM  c_order WHERE  state >1 AND state <=4 "; //今日
		$sql_yestoday = " SELECT SUM(order_money) as money_y FROM  c_order WHERE  state >1 AND state <=4 "; // 昨日

		if($day == 'week'){
			$sql_today .= " AND YEARWEEK(DATE_FORMAT(create_date,'%Y-%m-%d')) = YEARWEEK(NOW()) ";
			$sql_yestoday.=" AND YEARWEEK(DATE_FORMAT(create_date,'%Y-%m-%d')) = YEARWEEK(NOW())-1 ";
		}else if($day == 'month'){
			$sql_today.=" AND DATE_FORMAT(create_date,'%Y-%m')=DATE_FORMAT(now(),'%Y-%m') ";
			$sql_yestoday.=" AND DATE_FORMAT(create_date,'%Y-%m')=DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),'%Y-%m') ";
		}else{
			$sql_today.=' AND TO_DAYS(create_date) = TO_DAYS(NOW()) ';
			$sql_yestoday .=' AND TO_DAYS( NOW( ) ) - TO_DAYS( create_date) =1 ';
		}

		$money_total = DB::select($sql_total);
		$money_today = DB::select($sql_today);
		$money_yestoday = DB::select($sql_yestoday);
		if(!$money_total)$money_total =  array();
		if(!$money_today)$money_today =  array();
		if(!$money_yestoday)$money_yestoday = array();
		$data['money_total'] = isset($money_total[0]->money_total) ? $money_total[0]->money_total: 0 ;
		$data['money_today'] = isset($money_today[0]->money_t) ? $money_today[0]->money_t: 0 ;
		$data['money_yestoday'] = isset($money_yestoday[0]->money_y) ? $money_yestoday[0]->money_y: 0 ;
		$data['money_difference'] =$data['money_today'] - $data['money_yestoday'];
		return $data;
	}

	// 当日注册量
	public static function getCurrentDayRegister($day = 'today'){
		$data = array();
		$sql_total = "SELECT COUNT(*) as register_total FROM c_user_account  ";

		$register_total = DB::select($sql_total);
		if(!$register_total)$register_total =  array();
		$data['register_total'] = isset($register_total[0]->register_total) ? $register_total[0]->register_total: 0 ;
		return $data;
	}

	// 获取网点名称及经纬度
	public static function getBankShopLat(){
		$data = DB::table('bank_show')
			->where('state','!=',-1)
			->select('shopName','longitude','latitude','state')
			->get()
			->toArray();
		if(!$data){
			return array();
		}
		return $data;
	}

	// 获取网点总数、各网点的下单数
	public static function getEveryBankShopOrder($limit = ''){
		$orderArray = DB::table('bank_show')
		->leftjoin('order', 'order.bank_id', '=', 'bank_show.id')
		->where('bank_show.state','!=',-1)
		->groupby('bank_show.id','bank_show.shopName','bank_show.state')
		->orderby('money_total','DESC')
		->take($limit)
		->get(['bank_show.id','bank_show.state','bank_show.shopName',DB::raw('COUNT(bank_id) as order_total'),DB::raw('sum(order_money) as money_total')])
		->toArray();
		return $orderArray;

	}

	// 获取交易总数
	public static function getCurrentDayTotal(){
		$sql = "SELECT COUNT(*) as total FROM c_order AS a 
			LEFT JOIN c_user_account AS b ON a.open_id=b.open_id 
			WHERE  ( a.state >1 AND a.state <=4 ) AND  TO_DAYS(a.create_date) = TO_DAYS(NOW())  ";
		$orderInfo = DB::select($sql);
		if(!$orderInfo) return 0;
		return $orderInfo[0]->total;
	}


	// 今日下单交易明细        TO_DAYS(a.create_date) = TO_DAYS(NOW())——》今日    TO_DAYS( NOW( ) ) - TO_DAYS(a.create_date) = 1 ——》昨日
	public static function getCurrentDayOrderInfo(){
		$sql = "SELECT a.order_no,a.car_code,b.phone,a.create_date,a.state,a.finish_date,a.order_money FROM c_order AS a 
		LEFT JOIN c_user_account AS b ON a.open_id=b.open_id 
		WHERE  ( a.state >1 AND a.state <=4 ) AND 
		TO_DAYS(a.create_date) = TO_DAYS(NOW())  
		";

//		if($offset == 0 || $offset){
//			$sql.=" Limit ".$offset.','.$num;
//		}
		$orderInfo = DB::select($sql);
		if(!$orderInfo) return array();
		return $orderInfo;
	}

	// 获取日订单时间点交易金额数据---折线图
	public static function getOrderMoneyDay(){
		$sql=" SELECT DATE_FORMAT(create_date, '%H') AS times ,COUNT(id) AS xdTotal ,SUM(order_money) AS xdTotalMoney  FROM c_order  
	    WHERE (state<=4 AND state>1 ) AND TO_DAYS(create_date) = TO_DAYS(NOW()) GROUP BY times
		";
		$orderData = DB::select($sql);
		if(!$orderData) return array();
		return $orderData;

	}






}
